# 1 下列有一个简单的任务，每个任务都会睡眠10秒，如果是用单线程运行，至少要运行100秒,请使用多线程启动10个线程运行，实现10秒左右运行完所有任务
#
# 任务代码：

import unittest,threading,time
import threadpool
from threadpool import ThreadPool, makeRequests


def test_task(name):
    print(f"{threading.current_thread().name}:",name)
    time.sleep(10)

class TestTask(unittest.TestCase):
    def test01(self):
        test_task(1)
    def test02(self):
        test_task(2)
    def test03(self):
        test_task(3)
    def test04(self):
        test_task(4)
    def test05(self):
        test_task(5)
    def test06(self):
        test_task(6)
    def test07(self):
        test_task(7)
    def test08(self):
        test_task(8)
    def test09(self):
        test_task(9)
    def test10(self):
        test_task(10)
def discover_case():
    #实例化测试套件
    suite = unittest.TestSuite()
    for i in range(1,11):
        suite.addTests([TestTask('test'+str(i).zfill(2))])
    return suite

def run_testsuite(suite):
    #实例化TextRunner
    runner = unittest.TextTestRunner()
    #使用runner运行测试套件
    result = runner.run(suite)
    return result




if __name__ == '__main__':
    #设置线程池
    pool = ThreadPool(10)
    #寻找测试套件
    suite = discover_case()
    #设置任务请求（执行测试用例run_testsuite)
    requests = threadpool.makeRequests(run_testsuite,suite)
    #把请求放入线程池中，运行起来
    for req in requests:
        print("每个线程运行的任务是什么：",req)
        pool.putRequest(req)
    pool.wait()


